/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package PurchaseOrder;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mukesh
 */
public class EditPurchaseOrder extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            PurchaseOrderBean pob = new PurchaseOrderBean();

            if (request.getParameter("id").length() != 0) {
                pob.setOrderId(Integer.parseInt(request.getParameter("id")));
            }
            if (request.getParameter("vendor").length() != 0) {
                pob.setVendorId(Integer.parseInt(request.getParameter("vendor")));
            }
            if (request.getParameter("storetype").length() != 0) {
                pob.setStoreTypeId(Integer.parseInt(request.getParameter("storetype")));
            }
            pob.setProductName(request.getParameter("pname"));

            if (request.getParameter("qty").length() != 0) {
                pob.setQty(Integer.parseInt(request.getParameter("qty")));
            }
            if (request.getParameter("unitprice").length() != 0) {
                pob.setUnitPrice(Integer.parseInt(request.getParameter("unitprice")));
            }
            if (request.getParameter("tax").length() != 0) {
                pob.setTax(Integer.parseInt(request.getParameter("tax")));
            }
            pob.settAmount(Integer.parseInt(request.getParameter("tamnt")));

            pob.setApprovalStatus(0);

            if (request.getParameter("approvedBy").length() != 0) {
                pob.setApprovedBy(Integer.parseInt(request.getParameter("approvedBy")));
            }
            if ((request.getParameter("pod").length() != 0)&&request.getParameter("pod")==null) {
                SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy");
                java.util.Date dt = formater.parse(request.getParameter("pod"));
                pob.setPurchaseOrderDate(new Date(dt.getTime()));
            }

            pob.setPurchaseOrderNo(request.getParameter("podNo"));
            if ((request.getParameter("approvedDate").length() != 0)&&request.getParameter("approvedDate")==null) {
                SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy");
                java.util.Date dt = formater.parse(request.getParameter("approvedDate"));
                pob.setApprovedDate(new Date(dt.getTime()));
            }

            if (request.getParameter("approvalNo").length() != 0) {
                pob.setApprovalNo(Integer.parseInt(request.getParameter("approvalNo")));
            }

            pob.setUserId(Integer.parseInt(session.getAttribute("uid").toString()));
            pob.setDescription(request.getParameter("desc"));
            int i = PurchaseOrder.Query.edit(pob);
            if (i != 0) {
                session.setAttribute("status", "Success");
                session.setAttribute("msg", "Order of (" + pob.getProductName() + ") Edited Successfully.");
                response.sendRedirect("/RIT/Services/Inventory/PurchaseOrder.jsp");
            } else {
                session.setAttribute("status", "Fail");
                session.setAttribute("msg", "Check Order Details and try again.");
                response.sendRedirect("/RIT/Services/Inventory/PurchaseOrder.jsp");
            }

        } catch (Exception e) {
            session.setAttribute("status", "Fail");
            session.setAttribute("msg", "Check Order Details and try again.(e)");
            response.sendRedirect("/RIT/Services/Inventory/PurchaseOrder.jsp");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
